#!/bin/bash
# rpmbuild shell
# author: ning.xue

IFS=""
#附加内容
rm -rf a8root/bin
mkdir -p a8root/bin/inke_replace_ads/
cp  ../src/inke_replace_ads_server.sh a8root/bin/inke_replace_ads/start.sh
cp  ../src/inke_replace_ads_manager.py a8root/bin/inke_replace_ads/inke_replace_ads_manager.py
cp  ../src/inke_replace_ads_handler.py a8root/bin/inke_replace_ads/inke_replace_ads_handler.py
cp  ../src/inke_replace_ads_server.py a8root/bin/inke_replace_ads/inke_replace_ads_server.py
cp  ../src/checkstr_legitimacy.py a8root/bin/inke_replace_ads/checkstr_legitimacy.py
#需要配置的内容：

#1:要发布的总版本号
VERSION=1.0.0

#2:要发布的子版本号
RELEASE=19

#打包成tar的文件
SERVICEFILE="
a8root/bin/inke_replace_ads/
"

#从下面开始如果不定义的变量一律写成
#X="
#"

#3"rpm中定义的变量"
RPMVAR="
%define DESTDIR /a8root/bin/inke_replace_ads/*
"

#4:RPM包需要写的内容
CHANGEVAR_CONTENTS=(
#descrpition:
'
'
#prep:
'
rm -rf ${RPM_BUILD_DIR}/%{name}-%{version}
rm -rf ${RPM_BUILD_ROOT}
%setup -c ${RPM_BUILD_DIR}/%{name}-%{version}
'
#build:
'
'
#install:
'
rm -rf ${RPM_BUILD_ROOT}
cp -R ${RPM_BUILD_DIR}/%{name}-%{version} ${RPM_BUILD_ROOT}
'
#clean:
'
rm -rf ${RPM_BUILD_ROOT}
'
#pre:
'
'
#post:
'
chown -R www:www /a8root/bin/inke_replace_ads/
cp /a8root/bin/inke_replace_ads/start.sh /etc/init.d/inke_replace_ads_serviced
chmod a+x /etc/init.d/inke_replace_ads_serviced
/sbin/chkconfig --level 345 inke_replace_ads_serviced on
'
#preun:
'
'
#postun:
'
'
#files:
'
%{DESTDIR}
'
)


#5:体系结构
#64位x86_64
#32位i386
#32位i686
ARCHITECTURE=x86_64

#6:个人打包路径
RPMPATH=$HOME/rpm

#7:服务名称
SERVICENAME=inke_replace_ads_service

#8:打包文件名称
SERVICESOURCE="${SERVICENAME}.tar"

#程序正文


SPECFILENAMEBASE=${0%.*}

SPECFILENAME=${SPECFILENAMEBASE}.spec


#生成文件
rm -rf ${SPECFILENAME}

cat << EOF >> ${SPECFILENAME}
Define:
Summary:
Name:
Version:
Release:
Group:The duomi Group
URL:http://www.duomi.com
Packager:ning.xue
License:DUOMI
Source:
Buildroot: %{_tmppath}/%{name}-%{version}

rpm_ning01

rpm_ning02

rpm_ning03

rpm_ning04

rpm_ning05

rpm_ning06

rpm_ning07

rpm_ning08

rpm_ning09

rpm_ning10

EOF

TMPVAR=""

if [ X"${RPMVAR}" = X"
    " ];then
    sed -i -e "s@Define:@@g" ${SPECFILENAME}
else
    TMPVAR=$(echo ${RPMVAR} | sed ":a;N;s/\n/###/g;ta")
    sed -i -e "s@Define:@${TMPVAR}@g" ${SPECFILENAME}
fi

sed -i -e "s#Summary:#&${SERVICENAME} service#g" \
    -e "s#Name:#&${SERVICENAME}#g" \
    -e "s#Version:#&${VERSION}#g" \
    -e "s#Release:#&${RELEASE}#g" \
    -e "s#Source:#&${SERVICESOURCE}#g" ${SPECFILENAME}

CHANGEVARS_INST=(
"rpm_ning01"
"rpm_ning02"
"rpm_ning03"
"rpm_ning04"
"rpm_ning05"
"rpm_ning06"
"rpm_ning07"
"rpm_ning08"
"rpm_ning09"
"rpm_ning10"
)

CHANGEVARS=(
"%description"
"%prep"
"%build"
"%install"
"%clean"
"%pre"
"%post"
"%preun"
"%postun"
"%files"
)


for((i=0;i<${#CHANGEVARS[@]};i++))
do
    echo ${CHANGEVAR_CONTENTS[i]}
    if [ ! X"${CHANGEVAR_CONTENTS[i]}" == X"
        " ];then
        TMPVAR=$(echo ${CHANGEVAR_CONTENTS[i]} | sed ":a;N;s/\n/###/g;ta")
        sed -i -e "{s@${CHANGEVARS_INST[i]}@${CHANGEVARS[i]}${TMPVAR}@g}" ${SPECFILENAME}
    else
        sed -i -e "{s@${CHANGEVARS_INST[i]}@${CHANGEVARS[i]}@g}" ${SPECFILENAME}
    fi
done

sed -i -e "s/###/\n/g" ${SPECFILENAME}

IFS="
"
rm -rf $RPMPATH/SOURCES/*.*
rm -rf $RPMPATH/RPMS/${ARCHITECTURE}/${SERVICESOURCE}

tar cvf $RPMPATH/SOURCES/$SERVICESOURCE $SERVICEFILE


rpmbuild -bb ${SPECFILENAME}

ls $RPMPATH/RPMS/${ARCHITECTURE}/${SERVICENAME}-*.${ARCHITECTURE}.rpm | sed 's/.rpm$/.tmp/' |xargs touch

find $RPMPATH/RPMS/${ARCHITECTURE}/  -name "*debuginfo*" |xargs rm -rf

#./send.expect rpmclient rpmclient 118.192.170.50 ${RPMPATH}/RPMS/${ARCHITECTURE}/${SERVICENAME}-${VERSION}-${RELEASE}.${ARCHITECTURE}.rpm /home/rpmclient
#./send.expect rpmclient rpmclient 118.192.170.50 ${RPMPATH}/RPMS/${ARCHITECTURE}/${SERVICENAME}-${VERSION}-${RELEASE}.${ARCHITECTURE}.tmp /home/rpmclient

echo "打包成功"

rm -rf $SPECFILENAME
